package com.ourteam.product.requirement.dao.impl;

import com.ourteam.product.requirement.dao.IProductVersionDAO;
import com.ourteam.product.requirement.dao.ProductVersion;
import com.ourteam.product.requirement.dao.ProductVersionDAOQueryBean;

import net.dao.AbstractSpringDAO;
import net.dao.DataAccessObjectHelper;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;

import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * Template Name : DAO服务对象
 * Template Version : 1
 * Template Last Modfiy Date : Fri Nov 17 11:00:11 CST 2017
 * Auto Generate Date : 2018-05-03 16:45:37
  * Warning ! This file will be overwrite when regenerate !
  *
 */
public class AbstractProductVersionDAOJdbcImpl extends AbstractSpringDAO
    implements IProductVersionDAO {
    public static final String[] ENTITY_FIELDS = new String[] {
            Id, ProductDefineId, ProductVersion, Remarks, Status,
            ProductVersionType, ParentVersionId, StartDate, EndDate,
            ActualStartDate, ActualEndDate, SortIndex, ProductName, ProductCdoe,
            ProductStatus, ProductRemarks, ParentProductVersion,
            ParentProductVersionType, ParentProductVersionStatus
        };
    public static final String[] ENTITY_PROPERTIES = new String[] {
            _DataObjectProperty.Id, _DataObjectProperty.ProductDefineId,
            _DataObjectProperty.ProductVersion, _DataObjectProperty.Remarks,
            _DataObjectProperty.Status, _DataObjectProperty.ProductVersionType,
            _DataObjectProperty.ParentVersionId, _DataObjectProperty.StartDate,
            _DataObjectProperty.EndDate, _DataObjectProperty.ActualStartDate,
            _DataObjectProperty.ActualEndDate, _DataObjectProperty.SortIndex,
            _DataObjectProperty.ProductName, _DataObjectProperty.ProductCdoe,
            _DataObjectProperty.ProductStatus,
            _DataObjectProperty.ProductRemarks,
            _DataObjectProperty.ParentProductVersion,
            _DataObjectProperty.ParentProductVersionType,
            _DataObjectProperty.ParentProductVersionStatus
        };
    public static final String[] ENTITY_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_AUTO_GEN_PK_FIELDS = new String[] { Id };
    public static final String[] ENTITY_UNIQUE_FIELDS = new String[] {  };
    private static String relatedJoinSQL = null;
    private static String basicFilterSQL = null;
    private DataAccessObjectHelper<ProductVersion, ProductVersionDAOQueryBean> daoHelper;

    public AbstractProductVersionDAOJdbcImpl() {
        super();
        daoHelper = new DataAccessObjectHelper<ProductVersion, ProductVersionDAOQueryBean>(this);
    }

    protected String getEntityName() {
        return EntityName;
    }

    protected String[] getEntityFields() {
        return ENTITY_FIELDS;
    }

    protected String[] getEntityProperties() {
        return ENTITY_PROPERTIES;
    }

    protected String[] getEntityPKFields() {
        return ENTITY_PK_FIELDS;
    }

    protected String[] getEntityAutoGenPKFields() {
        return ENTITY_AUTO_GEN_PK_FIELDS;
    }

    protected String[] getEntityUniqueFields() {
        return ENTITY_UNIQUE_FIELDS;
    }

    protected String getBaiscQueryFilter() {
        if (basicFilterSQL == null) {
            StringBuffer basicFilterSQLbuffer = new StringBuffer();

            basicFilterSQL = basicFilterSQLbuffer.toString();
        }

        return basicFilterSQL;
    }

    protected String getRelatedJoinSQL() {
        if (relatedJoinSQL == null) {
            StringBuffer relatedJoinSQLBuffer = new StringBuffer();

            relatedJoinSQLBuffer.append(" LEFT JOIN ")
                                .append(com.ourteam.product.requirement.dao.IProductDefineDAO.EntityName)
                                .append(" PRODUCT_DEFINE ");

            relatedJoinSQLBuffer.append(" ON ( ");

            relatedJoinSQLBuffer.append(StringUtils.replace(
                    com.ourteam.product.requirement.dao.IProductDefineDAO.Id,
                    com.ourteam.product.requirement.dao.IProductDefineDAO.EntityName +
                    ".", "PRODUCT_DEFINE."));
            relatedJoinSQLBuffer.append(" = ").append(ProductDefineId);

            relatedJoinSQLBuffer.append(" ) ");

            relatedJoinSQLBuffer.append(" LEFT JOIN ")
                                .append(com.ourteam.product.requirement.dao.IProductVersionDAO.EntityName)
                                .append(" RELATED_X9KVSF_ProductVersion ");

            relatedJoinSQLBuffer.append(" ON ( ");

            relatedJoinSQLBuffer.append(StringUtils.replace(
                    com.ourteam.product.requirement.dao.IProductVersionDAO.Id,
                    com.ourteam.product.requirement.dao.IProductVersionDAO.EntityName +
                    ".", "RELATED_X9KVSF_ProductVersion."));
            relatedJoinSQLBuffer.append(" = ").append(ParentVersionId);

            relatedJoinSQLBuffer.append(" ) ");

            relatedJoinSQL = relatedJoinSQLBuffer.toString();
        }

        return relatedJoinSQL;
    }

    /**
         * Check Data Unique
         * @param aProductVersion
         * @throws Exception
         */
    public boolean checkIsUnique(ProductVersion aProductVersion)
        throws Exception {
        if (aProductVersion == null) {
            return true;
        }

        List valueList = new ArrayList();

        List pkValueList = new ArrayList();

        pkValueList.add(aProductVersion.getId());

        return isUnique(valueList.toArray(new Object[valueList.size()]),
            pkValueList.toArray(new Object[pkValueList.size()]));
    }

    /**
     * Insert Data
     * @param aProductVersion
     * @throws Exception
     */
    public void insert(ProductVersion aProductVersion)
        throws Exception {
        super.doInsert(aProductVersion);
    }

    /**
         * Batch Insert Datas
         * @param aProductVersion
         * @throws Exception
         */
    public void batchInsert(ProductVersion[] aProductVersions)
        throws Exception {
        super.doBatchInsert(aProductVersions);
    }

    /**
         * Delete Data
         * @param  final long id         *
         * @throws Exception
         */
    public void delete(final long id) throws Exception {
        super.doDeleteById(new Object[] { new Long(id) });
    }

    /**
     * Delete Datas
     * @param queryBean
     * @throws Exception
     */
    public void delete(ProductVersionDAOQueryBean queryBean)
        throws Exception {
        super.doDeleteByQuery(queryBean);
    }

    public void delete(ProductVersion aProductVersion)
        throws Exception {
        delete(aProductVersion.getId());
    }

    /**
     * Update Data Selective
     * @param aProductVersion
     * @throws Exception
     */
    public void update(ProductVersion aProductVersion)
        throws Exception {
        if (aProductVersion.getId() == 0) {
            throw new Exception(
                "Can not update object data whith out primary key value");
        }

        super.doUpdate(aProductVersion);

        ProductVersion tempObj = findById(aProductVersion.getId());

        if (tempObj != null) {
            PropertyUtils.copyProperties(aProductVersion, tempObj);
        }
    }

    /**
     * Batch Update Datas
     * @param aProductVersions[]
     * @throws Exception
     */
    public void batchUpdate(ProductVersion[] aProductVersions)
        throws Exception {
        super.doBatchUpdate(aProductVersions);
    }

    /**
         * Update Data By Condictions
         * @param aProductVersion
         * @param aValueCriteria
         * @param aTransaction
         * @throws Exception
         */
    public void update(ProductVersion aProductVersion,
        ProductVersionDAOQueryBean aQueryBean) throws Exception {
        super.doUpdateByQuery(aProductVersion, aQueryBean);
    }

    /**
     * Update Data By Condictions
     * @param aQueryBean
     * @throws Exception
     */
    public void update(ProductVersionDAOQueryBean aQueryBean)
        throws Exception {
        super.doUpdateByQuery(aQueryBean);
    }

    protected void checkNullValue(ProductVersion aProductVersion)
        throws Exception {
        if (aProductVersion.getProductName() == null) {
            throw new Exception(
                "The ProductVersion.productName value is not allow NULL");
        }

        if (aProductVersion.getProductCdoe() == null) {
            throw new Exception(
                "The ProductVersion.productCdoe value is not allow NULL");
        }

        if (aProductVersion.getProductStatus() == null) {
            throw new Exception(
                "The ProductVersion.productStatus value is not allow NULL");
        }

        if (aProductVersion.getStartDate() == null) {
            throw new Exception(
                "The ProductVersion.startDate value is not allow NULL");
        }

        if (aProductVersion.getEndDate() == null) {
            throw new Exception(
                "The ProductVersion.endDate value is not allow NULL");
        }

        if (aProductVersion.getActualStartDate() == null) {
            throw new Exception(
                "The ProductVersion.actualStartDate value is not allow NULL");
        }

        if (aProductVersion.getActualEndDate() == null) {
            throw new Exception(
                "The ProductVersion.actualEndDate value is not allow NULL");
        }

        if (aProductVersion.getRemarks() == null) {
            throw new Exception(
                "The ProductVersion.remarks value is not allow NULL");
        }

        if (aProductVersion.getStatus() == null) {
            throw new Exception(
                "The ProductVersion.status value is not allow NULL");
        }
    }

    /**
     * Get Data By ID
     * @param  final long id         * @return  ${businessObject.name}
     * @throws Exception
     */
    public ProductVersion findById(final long id) throws Exception {
        ProductVersionDAOQueryBean queryBean = new ProductVersionDAOQueryBean();

        ProductVersionDAOQueryBean.Criteria criteria = queryBean.createCriteria();

        criteria.andIdEqualTo(id);

        //queryBean.setDistinct(true);
        ProductVersion[] ProductVersions = query(queryBean);

        if (ProductVersions.length == 0) {
            return null;
        } else {
            return ProductVersions[0];
        }
    }

    /**
         * Query Data
         * @param queryBean
         * @return ProductVersion[]
         * @throws Exception
         */
    public ProductVersion[] query(ProductVersionDAOQueryBean queryBean)
        throws Exception {
        List result = super.doQuery(queryBean, new JdbcProductVersionRowMapper());

        return (ProductVersion[]) result.toArray(new ProductVersion[result.size()]);
    }

    /**
     * Query ${businessObject.name} Selective
     * @param queryBean
     * @return Map[]
     * @model
     * @throws Exception
     */
    public Map<String, Object>[] querySelective(
        ProductVersionDAOQueryBean queryBean) throws Exception {
        if (queryBean.getSelectProperties().isEmpty()) {
            queryBean.addAllSelectProperties();
        }

        return super.doQuerySelective(queryBean);
    }

    /**
     * Query Data Count
     * @param queryBean
     * @return int
     * @throws Exception
     */
    public int queryCount(ProductVersionDAOQueryBean queryBean)
        throws Exception {
        return super.doQueryCount(queryBean);
    }

    public void synchronize(ProductVersion[] aProductVersions,
        ProductVersionDAOQueryBean queryBean) throws Exception {
        daoHelper.synchronize(aProductVersions, queryBean);
    }

    public void synchronize(ProductVersion[] aProductVersions,
        ProductVersionDAOQueryBean queryBean,
        net.dao.IDataAccessInterface.ISynchronizeCallback<ProductVersion> callBack)
        throws Exception {
        daoHelper.synchronize(aProductVersions, queryBean, callBack);
    }

    public static class JdbcProductVersionRowMapper implements RowMapper<ProductVersion> {
        public ProductVersion mapRow(ResultSet resultSet, int arg1)
            throws SQLException {
            ProductVersion object = new ProductVersion();

            int columnIndex = 1;

            int __dataType;

            try {
                Object id = null;

                id = resultSet.getObject(columnIndex++);

                object.setIdNull(resultSet.getBoolean(columnIndex++));

                if (id != null) {
                    setBeanValue(object, _DataObjectProperty.Id, id);
                }

                Object productDefineId = null;

                productDefineId = resultSet.getObject(columnIndex++);

                object.setProductDefineIdNull(resultSet.getBoolean(
                        columnIndex++));

                if (productDefineId != null) {
                    setBeanValue(object, _DataObjectProperty.ProductDefineId,
                        productDefineId);
                }

                Object productVersion = null;

                productVersion = resultSet.getObject(columnIndex++);

                object.setProductVersionNull(resultSet.getBoolean(columnIndex++));

                if (productVersion != null) {
                    setBeanValue(object, _DataObjectProperty.ProductVersion,
                        productVersion);
                }

                Object remarks = null;

                remarks = resultSet.getObject(columnIndex++);

                object.setRemarksNull(resultSet.getBoolean(columnIndex++));

                if (remarks != null) {
                    setBeanValue(object, _DataObjectProperty.Remarks, remarks);
                }

                Object status = null;

                status = resultSet.getObject(columnIndex++);

                object.setStatusNull(resultSet.getBoolean(columnIndex++));

                if (status != null) {
                    setBeanValue(object, _DataObjectProperty.Status, status);
                }

                Object productVersionType = null;

                productVersionType = resultSet.getObject(columnIndex++);

                object.setProductVersionTypeNull(resultSet.getBoolean(
                        columnIndex++));

                if (productVersionType != null) {
                    setBeanValue(object,
                        _DataObjectProperty.ProductVersionType,
                        productVersionType);
                }

                Object parentVersionId = null;

                parentVersionId = resultSet.getObject(columnIndex++);

                object.setParentVersionIdNull(resultSet.getBoolean(
                        columnIndex++));

                if (parentVersionId != null) {
                    setBeanValue(object, _DataObjectProperty.ParentVersionId,
                        parentVersionId);
                }

                Object startDate = null;

                __dataType = resultSet.getMetaData().getColumnType(columnIndex);

                if ((__dataType == java.sql.Types.DATE) ||
                        (__dataType == java.sql.Types.TIME) ||
                        (__dataType == java.sql.Types.TIMESTAMP)) {
                    startDate = resultSet.getTimestamp(columnIndex++);
                } else {
                    startDate = resultSet.getObject(columnIndex++);
                }

                object.setStartDateNull(resultSet.getBoolean(columnIndex++));

                if (startDate != null) {
                    setBeanValue(object, _DataObjectProperty.StartDate,
                        startDate);
                }

                Object endDate = null;

                __dataType = resultSet.getMetaData().getColumnType(columnIndex);

                if ((__dataType == java.sql.Types.DATE) ||
                        (__dataType == java.sql.Types.TIME) ||
                        (__dataType == java.sql.Types.TIMESTAMP)) {
                    endDate = resultSet.getTimestamp(columnIndex++);
                } else {
                    endDate = resultSet.getObject(columnIndex++);
                }

                object.setEndDateNull(resultSet.getBoolean(columnIndex++));

                if (endDate != null) {
                    setBeanValue(object, _DataObjectProperty.EndDate, endDate);
                }

                Object actualStartDate = null;

                __dataType = resultSet.getMetaData().getColumnType(columnIndex);

                if ((__dataType == java.sql.Types.DATE) ||
                        (__dataType == java.sql.Types.TIME) ||
                        (__dataType == java.sql.Types.TIMESTAMP)) {
                    actualStartDate = resultSet.getTimestamp(columnIndex++);
                } else {
                    actualStartDate = resultSet.getObject(columnIndex++);
                }

                object.setActualStartDateNull(resultSet.getBoolean(
                        columnIndex++));

                if (actualStartDate != null) {
                    setBeanValue(object, _DataObjectProperty.ActualStartDate,
                        actualStartDate);
                }

                Object actualEndDate = null;

                __dataType = resultSet.getMetaData().getColumnType(columnIndex);

                if ((__dataType == java.sql.Types.DATE) ||
                        (__dataType == java.sql.Types.TIME) ||
                        (__dataType == java.sql.Types.TIMESTAMP)) {
                    actualEndDate = resultSet.getTimestamp(columnIndex++);
                } else {
                    actualEndDate = resultSet.getObject(columnIndex++);
                }

                object.setActualEndDateNull(resultSet.getBoolean(columnIndex++));

                if (actualEndDate != null) {
                    setBeanValue(object, _DataObjectProperty.ActualEndDate,
                        actualEndDate);
                }

                Object sortIndex = null;

                sortIndex = resultSet.getObject(columnIndex++);

                object.setSortIndexNull(resultSet.getBoolean(columnIndex++));

                if (sortIndex != null) {
                    setBeanValue(object, _DataObjectProperty.SortIndex,
                        sortIndex);
                }

                Object productName = null;

                productName = resultSet.getObject(columnIndex++);

                object.setProductNameNull(resultSet.getBoolean(columnIndex++));

                if (productName != null) {
                    setBeanValue(object, _DataObjectProperty.ProductName,
                        productName);
                }

                Object productCdoe = null;

                productCdoe = resultSet.getObject(columnIndex++);

                object.setProductCdoeNull(resultSet.getBoolean(columnIndex++));

                if (productCdoe != null) {
                    setBeanValue(object, _DataObjectProperty.ProductCdoe,
                        productCdoe);
                }

                Object productStatus = null;

                productStatus = resultSet.getObject(columnIndex++);

                object.setProductStatusNull(resultSet.getBoolean(columnIndex++));

                if (productStatus != null) {
                    setBeanValue(object, _DataObjectProperty.ProductStatus,
                        productStatus);
                }

                Object productRemarks = null;

                productRemarks = resultSet.getObject(columnIndex++);

                object.setProductRemarksNull(resultSet.getBoolean(columnIndex++));

                if (productRemarks != null) {
                    setBeanValue(object, _DataObjectProperty.ProductRemarks,
                        productRemarks);
                }

                Object parentProductVersion = null;

                parentProductVersion = resultSet.getObject(columnIndex++);

                object.setParentProductVersionNull(resultSet.getBoolean(
                        columnIndex++));

                if (parentProductVersion != null) {
                    setBeanValue(object,
                        _DataObjectProperty.ParentProductVersion,
                        parentProductVersion);
                }

                Object parentProductVersionType = null;

                parentProductVersionType = resultSet.getObject(columnIndex++);

                object.setParentProductVersionTypeNull(resultSet.getBoolean(
                        columnIndex++));

                if (parentProductVersionType != null) {
                    setBeanValue(object,
                        _DataObjectProperty.ParentProductVersionType,
                        parentProductVersionType);
                }

                Object parentProductVersionStatus = null;

                parentProductVersionStatus = resultSet.getObject(columnIndex++);

                object.setParentProductVersionStatusNull(resultSet.getBoolean(
                        columnIndex++));

                if (parentProductVersionStatus != null) {
                    setBeanValue(object,
                        _DataObjectProperty.ParentProductVersionStatus,
                        parentProductVersionStatus);
                }

                return object;
            } catch (Exception e) {
                throw new SQLException(e.getMessage());
            }
        }
    }
}
